package com.keycloak.admin.utils;

import com.alibaba.fastjson.JSONArray;
import com.keycloak.admin.config.Myprops;
import com.keycloak.admin.model.params.KeycloakDriverParams;
import com.keycloak.admin.model.params.RegParams;
import com.keycloak.admin.model.params.vo.ComplateDriverInfoVerifyVo;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MainServerUtils {

    @Resource
    Myprops myProps;

    public Boolean driverLoginForWanganPush(String cell, String clientId, RegParams regParams,HttpServletRequest request1){
        try {
            System.out.println("开始网安登录推送！");
            String Url=myProps.getMainServerUrl();
            String url=Url+"/ydxapp/keycloakUserLogin";
            System.out.println(url);
            Map<String, Map<String, Object>> params = new HashMap();//请求参数体
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("cell",cell);
            map.put("clientId",clientId);
            map.put("ip",request1.getRemoteAddr());
            map.put("port",request1.getRemotePort());
            Map<String, Object> promap = new HashMap<String, Object>();//协议说明
            promap.put("appkey", "");
            promap.put("channel", "XIAOMI");
            promap.put("token", "");
            promap.put("ver", "1.0");
            promap.put("lat",regParams.getLat());
            promap.put("lng",regParams.getLng());
            promap.put("os",regParams.getOs());

            params.put("params", map);
            params.put("protocol", promap);
            HttpRequester httpRequester = new HttpRequester();
            System.out.println(url);
            String  result = null;
            try {
                result = PayCallBackUtil.net(url, params, "POST",null);
                JSONObject object = JSONObject.fromObject(result);
                if (object.getInt("code") == 0) {

                    return true;
                } else {
                    System.out.println(object.get("code") + ":" + object.get("msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<KeycloakDriverParams> removeUser(){
        try {
            String Url=myProps.getMainServerUrl();
            String url=Url+"/driverapp/order/getDriverListNotKeycloak";
            System.out.println(url);
            Map<String, Map<String, Object>> params = new HashMap();//请求参数体
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id","");
            Map<String, Object> promap = new HashMap<String, Object>();//协议说明
            promap.put("appkey", "");
            promap.put("channel", "XIAOMI");
            promap.put("token", "");
            promap.put("ver", "1.0");

            params.put("params", map);
            params.put("protocol", promap);
            HttpRequester httpRequester = new HttpRequester();
            System.out.println(url);
            String  result = null;
            try {
                result = PayCallBackUtil.net(url, params, "POST",null);
                JSONObject object = JSONObject.fromObject(result);
                if (object.getInt("code") == 0) {
                    Object data = object.get("data");
                    //JSONObject jsonObject = JSONObject.fromObject(data);
                    List<KeycloakDriverParams> keycloakDriverParams = JSONArray.parseArray(data.toString(), KeycloakDriverParams.class);
                  /*  JSONObject.
                    ComplateDriverInfoVerifyVo o = (ComplateDriverInfoVerifyVo)JSONObject.toBean(jsonObject, ComplateDriverInfoVerifyVo.class);
*/
                    return keycloakDriverParams;
                } else {
                    System.out.println(object.get("code") + ":" + object.get("msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public  ComplateDriverInfoVerifyVo sendOrderToMainServer(String str,String cell){
        try {
            String Url=myProps.getMainServerUrl();
            String url=Url+"/driverapp/driver/getComplateDriverInfoVerify";
            System.out.println(url);
            Map<String, Map<String, Object>> params = new HashMap();//请求参数体
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("phone",cell);
            Map<String, Object> promap = new HashMap<String, Object>();//协议说明
            promap.put("appkey", "");
            promap.put("channel", "XIAOMI");
            promap.put("token", "");
            promap.put("ver", "1.0");

            params.put("params", map);
            params.put("protocol", promap);
            HttpRequester httpRequester = new HttpRequester();
            System.out.println(url);
            String  result = null;
            try {
                result = PayCallBackUtil.net(url, params, "POST",null);
                JSONObject object = JSONObject.fromObject(result);
                if (object.getInt("code") == 0) {
                    Object data = object.get("data");
                    JSONObject jsonObject = JSONObject.fromObject(data);
                    ComplateDriverInfoVerifyVo o = (ComplateDriverInfoVerifyVo)JSONObject.toBean(jsonObject, ComplateDriverInfoVerifyVo.class);

                    return o;
                } else {
                    System.out.println(object.get("code") + ":" + object.get("msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public  Boolean userReg(String cell,String uuid,String clientId){
        try {
            String Url=myProps.getMainServerUrl();
            String url=Url+"/driverapp/user/regForkeycloak";
            System.out.println(url);
            Map<String, Map<String, Object>> params = new HashMap();//请求参数体
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("cell",cell);
            map.put("uuid",uuid);
            map.put("clientId",clientId);
            Map<String, Object> promap = new HashMap<String, Object>();//协议说明
            promap.put("appkey", "");
            promap.put("channel", "XIAOMI");
            promap.put("token", "");
            promap.put("ver", "1.0");

            params.put("params", map);
            params.put("protocol", promap);
            HttpRequester httpRequester = new HttpRequester();
            System.out.println(url);
            String  result = null;
            try {
                result = PayCallBackUtil.net(url, params, "POST",null);
                JSONObject object = JSONObject.fromObject(result);
                if (object.getInt("code") == 0) {

                    return true;
                } else {
                    System.out.println(object.get("code") + ":" + object.get("msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public  Boolean passengerReg(String cell){
        try {
            String Url=myProps.getMainServerUrl();
            String url=Url+"/passenger/regNewForKeycloakPassenger";
            System.out.println(url);
            Map<String, Map<String, Object>> params = new HashMap();//请求参数体
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("cell",cell);
          /*  map.put("uuid",uuid);
            map.put("clientId",clientId);*/
            Map<String, Object> promap = new HashMap<String, Object>();//协议说明
            promap.put("appkey", "");
            promap.put("channel", "XIAOMI");
            promap.put("token", "");
            promap.put("ver", "1.0");

            params.put("params", map);
            params.put("protocol", promap);
            HttpRequester httpRequester = new HttpRequester();
            System.out.println(url);
            String  result = null;
            try {
                result = PayCallBackUtil.net(url, params, "POST",null);
                JSONObject object = JSONObject.fromObject(result);
                if (object.getInt("code") == 0) {

                    return true;
                } else {
                    System.out.println(object.get("code") + ":" + object.get("msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

/*Unirest.setTimeouts(10000, 10000);
           HttpResponse<String> response =

           Unirest.post(myProps.getKeycloakUrl()+"/auth/realms/"+realmName+"/protocol/openid-connect/token/introspect")
                   .header("Content-Type", "application/x-www-form-urlencoded")
                   .field("client_id", clientId)
                   .field("client_secret", clientSecrt)
                   .field("token", token)
                   .asString();*/


    public  Boolean keycloakGetuserIno(String realmName,String clientId,String clientSecrt,String token){
        try {
            String Url=myProps.getKeycloakUrl();
            String url=Url+"/auth/realms/"+realmName+"/protocol/openid-connect/token/introspect";
            System.out.println(url);
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("token",token);
            map.put("client_secret",clientSecrt);
            map.put("client_id",clientId);

            HttpRequester httpRequester = new HttpRequester();
            System.out.println(url);
            String  result = null;
            try {
                result = PayCallBackUtil.net1(url, map, "POST",token);
                JSONObject object = JSONObject.fromObject(result);
                if (object.getInt("code") == 0) {

                    return true;
                } else {
                    System.out.println(object.get("code") + ":" + object.get("msg"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
